/*      需要引入fusionCharts.js
 * 		画柱状图
        createFlash(u1,"bar","div1",p2+"年登记翻供整体趋势图","数量","NAME",300,200);\\name是数据库中的字段
  		画折线图
  		createFlash(u2,"line","div2",p2+"年登记翻供整体趋势图","数量","月份","99%",h);
  		画饼图
  		createFlash(u2,"pie","div3",p2+"年登记翻供整体趋势图","数量","月份",w/2,h);
 */

var colours = [ "0000FF", "FF0000", "009900", "D59291", "B7CD92", "3E6BA2",
		"A43E3C", "84A249", "725991", "459DB6", "E4863F", "94ABD5" ];
var chose ;
function createFlash(u, type, divid, bt, yaixName, xaixKey, w, h, div2id, fh,callback) {
	var swfname = "";
	chose =fh;
	var json1 = '{ "chart": { ' + ' "caption": "'
			+ bt
			+ '",'
			+ ' "yaxisname": "'
			+ yaixName
			+ '",'
			+ '"showlabels": "1",'
			+ '"showcolumnshadow": "0",'
			+ '"animation": "1",'
			+ ' "bgcolor": "ffffff, FFFFFF",'
			+ ' "canvasBgColor":"FFFFFF",'
			+ ' "numvdivlines": "10",'
			+ ' "formatnumberscale": "0",'
			+ ' "divlinealpha": "30",'
			+ ' "labelpadding": "4",'
			+ ' "baseFontSize": "13",'+  
			// '"alternatehgridcolor": "ff5904",'+
			' "yaxisvaluespadding": "10",'
			+ ' "showvalues": "1",'
			+ ' "rotatevalues": "0",'
			+ ' "valueposition": "Below",'
			+ ' "canvaspadding": "10"'
			+ ' }, "categories": [ { "category": [@labels]'
			+

			'} ], "dataset":[ @datas],  "styles": {         '
			+ ' "definition": [                                '
			+ '                {                               '
			+ '                    "type": "font",             '
			+ '                    "name": "CaptionFont",      '
			+ '                    "size": "14",               '
			+ '                    "color": "666666"           '
			+ '                },                              '
			+ '                {                               '
			+ '                    "type": "font",             '
			+ '                    "size": "14",               '
			+ '                    "name": "SubCaptionFont",   '
			+ '                    "bold": "0"                 '
			+ '                }                               '
			+ '            ],                                  '
			+ '            "application": [                    '
			+ '                {                               '
			+ '                    "toobject": "caption",      '
			+ '                    "styles": "CaptionFont"     '
			+ '                }                             '
			+ '                ,{"toobject":"Categories","styles":"SubCaptionFont"} '
			+ '            ]' + '        }' + ' }';

	if (type == "line") {
		swfname = "MSLine.swf";
	} else if (type == "bar") {
		swfname = "MSColumnLine3D.swf";
	} else if (type == "pie") {
		swfname = "Pie3D.swf";
		json1 = '{ "chart": { "caption": "'
				+ bt
				+ '", '
				+ ' "bgcolor": "ffffff, c1e0c1",'
				+ '  "palette": "2", "animation": "1", "formatnumberscale": "0","numberprefix": " ","pieslicedepth": "30", "startingangle": "125" }, '
				+ '"data": [@datas  ] ,'
				+ ' "styles": {         '
				+ ' "definition": [                                '
				+ '                {                               '
				+ '                    "type": "font",             '
				+ '                    "name": "CaptionFont",      '
				+ '                    "size": "15",               '
				+ '                    "color": "666666"           '
				+ '                },                              '
				+ '                {                               '
				+ '                    "type": "font",             '
				+ '                    "name": "SubCaptionFont",   '
				+ '                    "bold": "0"                 '
				+ '                }                               '
				+ '            ],                                  '
				+ '            "application": [                    '
				+ '                {                               '
				+ '                    "toobject": "caption",      '
				+ '                    "styles": "CaptionFont"     '
				+ '                },                              '
				+ '                {                               '
				+ '                    "toobject": "SubCaption",   '
				+ '                    "styles": "SubCaptionFont"  '
				+ '                },{"toobject":"categories","styles":"CaptionFont"}'
				+ '            ]' + '        }' + '}';
	}
	var chart1 = new FusionCharts(ctxPath + "/resource/js/fusion/" + swfname,
			divid + "id", w, h);

	var labels = "";
	var datas = "";
	var customLabels = [];
	$
			.ajax({
				url : u,
				type : 'POST',
				dataType : 'json',
				contentType : "application/json; charset=utf-8",
				error : function() {
					alert('Error loading json document');
				},
				success : function(e) {
					// do something with json
					
					e = e.result;
					
					if(callback)
					{
						var tmp = new Array();
						tmp.push(e)
						callback.apply(null,tmp);
					}
					// alert(e)
					if (e.length > 0) {
						var j = 0;
						if (type == "pie") {
							var value = '';
							for ( var k in e[0]) {

								for ( var i = 0; i < 1; i++) {
									datas += '{"label":"' + k + '","value":"'
											+ e[i][k]
											+ '","issliced": "0","color":"'
											+ colours[j] + '"},';
								}
								customLabels[customLabels.length] = k;
								j++
							}

							datas = datas.substring(0, datas.length - 1);
						} else {
							for ( var k in e[0]) {
								if (j < 1) {
									j++;
									continue;
								}
								var value = '';
								if (e[0][xaixKey]) {
									// labels +=
									// '{"label":"'+e[j]['+xaixKey+']+'"},';
									// {"seriesname": "Model A12","color":
									// "A66EDD","data":[]}
									datas += '{"seriesname": "' + k
											+ '" ,"color": "' + colours[j - 1]
											+ '","data":[@value]},';
									customLabels[customLabels.length] = k;
								} else {
								}
								for ( var i = 0; i < e.length; i++) {
									if (j == 1) {
										labels += '{"label":"' + e[i][xaixKey]
												+ '"},';

									}
									var js = "barLink('"+divid+j+"')";
									value += '{"value":' + e[i][k] + ',"link":"javascript:'+js+'"},';
								}
								value = value.substring(0, value.length - 1);
								datas = datas.replace("@value", value);
								j++;
							}
							labels = labels.substring(0, labels.length - 1);
							datas = datas.substring(0, datas.length - 1);
						}

						json1 = json1.replace("@labels", labels.toString());
						json1 = json1.replace("@datas", datas.toString());
						
						
						// alert(datas)
						 //alert(json1)
						// var json2='{ "chart": { "caption":
						// "'+bt+'","yaxisname": "数量", "bgcolor": "F7F7F7,
						// E9E9E9", "numvdivlines": "1", "divlinealpha": "30",
						// "labelpadding": "10", "yaxisvaluespadding": "10",
						// "showvalues": "1", "rotatevalues": "1",
						// "valueposition": "Below", "canvaspadding": "10" },
						// "categories": [ { "category": [ { "label": "2006" },
						// { "label": "2007" }, { "label": "2008" }, { "label":
						// "2009" }, { "label": "2010" }, { "label": "2011" } ]
						// } ], "dataset": [ { "seriesname": "Model A12",
						// "color": "A66EDD", "data": [ { "value": "35" }, {
						// "value": "42" }, { "value": "31" }, { "value": "28"
						// }, { "value": "34" }, { "value": "30" } ] }, {
						// "seriesname": "Model A15", "color": "F6BD0F", "data":
						// [ { "value": "22" }, { "value": "25" }, { "value":
						// "18" }, { "value": "22" }, { "value": "17" }, {
						// "value": "16" } ] } ]}';
						// chart1.setJSONData('{ "chart": { "caption":
						// "2013年登记翻供整体趋势图", "yaxisname": "数量", "bgcolor":
						// "F7F7F7, E9E9E9", "numvdivlines": "10",
						// "divlinealpha": "30", "labelpadding": "10",
						// "yaxisvaluespadding": "10", "showvalues": "1",
						// "rotatevalues": "1", "valueposition": "Below",
						// "canvaspadding": "10" }, "categories": [ {
						// "category":
						// [{"label":"4月"},{"label":"3月"},{"label":"2月"}]} ],
						// "dataset":[ {"seriesname": "4月" ,"color":
						// "#0000FF","data":[{"value":4},{"value":5},{"value":2}]},{"seriesname":
						// "3月" ,"color":
						// "#FF0000","data":[{"value":214},{"value":158},{"value":106}]},{"seriesname":
						// "2月" ,"color":
						// "#009900","data":[{"value":0},{"value":0},{"value":0}]}]
						// }');
						chart1.setJSONData(json1);
						chart1.render(divid);
						
						
						// 生成自定义标签
						// alert(customLabels)
						var labelsHtml = "";

						if (div2id) {

							for ( var i = 0; i < customLabels.length; i++) {
								labelsHtml += "<span style='width:15px;height:15px;margin-top:2px; background:#"
										+ colours[i]
										+ "'>&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;"
										+ customLabels[i];
								if (fh) {
									labelsHtml += "<br/>";
									// 竖排
								    //判断是否已经设置了位置
									if(document.getElementById(div2id).style.top)
									{
									
									}
									else
									{
										document.getElementById(div2id).style.top = h / 2;
									}
								} else {
									labelsHtml += "&nbsp;&nbsp;&nbsp;&nbsp;";
								}
							}
							
							if (type == "bar") {
								document.getElementById(div2id).style.background = "#c1e0c1";
							}

							if (fh) {
							} else {
								
								//计算长度
								var labWid = 100*customLabels.length;
								
								document.getElementById(div2id).style.width = labWid+10;
//								var ofw = document.getElementById(div2id).clientWidth;
//								if(ofw<215)
//								{
//									document.getElementById(div2id).style.width = 220;
//									ofw = 220;
//								}
								document.getElementById(div2id).style.left = w
										/ 2
										- labWid
										/ 2 + 5;
							}
							document.getElementById(div2id).innerHTML = labelsHtml;
						}
					} else {
						var o = document.getElementById(divid);
						o.innerHTML = "<span style='position: absolute;top:"+h/2+"px;left:"+(w/2-105)+"px;  font-size:18;font-family: 微软雅黑;'>当前没有查询到任何数据!</span>";
						o.style.width = w;
						o.style.height = h;
						o.style.background = '#c1e0c1';
						o.style.textAlign = "center";
					}
				}
			});
}


function barLink(divid)
{	
	if(chose==0)
	{	
		if(divid=="div1Left1"||divid=="div1Right1"||divid=="div1Right2")
		{
			try{
			parent.removeTab('1000196');
			}catch(e){}
			parent.createTab("1000196","入库管理","/sacw/caseGoods/relateCase/putStorage/1.html");
		}
		else 
			if(divid=="div1Left2")
			{
				parent.createTab("1000197","出库管理","/sacw/caseGoods/relateCase/outStorage.html");
			}
	}
}